#include <swilib.h>
#include "uReader.h"
#include "uCodePage.h"
#include "uMyDraw2.h"
#include "ft_font.h"

extern const char FONT_PATH[64];
int space_width;

int flags;
FT_Library       library;
FT_Face          face;
FT_Bitmap        bitmap;
FT_GlyphSlot     glyph;

void DrawSBit(int x, int y, FTC_SBit sbit)
{
  if(sbit->format == FT_PIXEL_MODE_GRAY)
  {
    y-=sbit->top;
    x+=sbit->left;
    char color[4];
    int ptr=0;
    for(int h=0; h < sbit->height; h++)
    {
      for(int w=0; w < sbit->width; w++)
      {
        color[3] = sbit->buffer[ptr]*100/sbit->max_grays;
        SetPixel(x+w,y+h,color);
        ptr++;
      }
    }
  }
  /*else
  {
    IMGHDR* img = malloc(sizeof(IMGHDR));
    img->bitmap = (char*)sbit->buffer;
    img->w = sbit->width;
    img->h = sbit->height;
    img->bpnum = 1;
    DrwImg(img, x, y, TEXT_COLOR, BG_COLOR);
    mfree(img);
  }*/
}

void TT_PutChar(int x, int y, TT_Char* symbol, char cl)
{
  if(!symbol->bitmap) return;
  y-=symbol->top;
  x+=symbol->left;
  int ptr=0;
  for(int h=0; h < symbol->height; h++)
  {
    for(int w=0; w < symbol->width; w++)
    {
      PutPixel(x+w,y+h,symbol->bitmap[ptr],cl);
      ptr++;
    }
  }
}

TT_Char* GetChar(short symb)
{
  FT_Error error = FT_Load_Char( face, symb, flags );
  if(error) return 0;
  
  FT_GlyphSlot slot = face->glyph;
  TT_Char* node = malloc(sizeof(TT_Char));
  node->width = slot->bitmap.width;
  node->height = slot->bitmap.rows;
  node->top = slot->bitmap_top;
  node->left = slot->bitmap_left;
  node->xadvance = slot->advance.x >> 6;
  int bmlen = node->width*node->height;
  node->bitmap = malloc(bmlen);
  memcpy(node->bitmap, slot->bitmap.buffer, bmlen);
  return node;
}

void TT_DrawString(int x, int y, char* text, char cl)
{
  int n, num_chars;
  
  if(!text || !*text) return;
  num_chars = strlen(text);
  
  for(n = 0; n < num_chars; n++)
  {
    TT_Char* node;
    short symb = CPM2Unicode (text[n],CP1251);
    node = TTC_RequestChar(symb);
    if(!node) continue;
    TT_PutChar(x, y+font_size, node, cl);
    x+=node->xadvance;
  }
}

int TT_GetCharWidth(char s)
{
  TT_Char* node;
  short symb = CPM2Unicode (s,CP1251);
  node = TTC_RequestChar(symb);
  if(!node) return 0;
  return(node->xadvance);
}

int TT_GetFontHeight()
{
  return font_size+1;
}

void initFont()
{
  flags = FT_LOAD_RENDER
    | (!CFG_FORCE_AUTOHINT ? FT_LOAD_FORCE_AUTOHINT : 0)
    | (!CFG_DISABLE_AUTOHINT ? FT_LOAD_NO_AUTOHINT : 0)
    | (CFG_LCD_AUTOHINT ? FT_RENDER_MODE_LCD_V : 0);
  int error = FT_Init_FreeType(&library);
  if(error)
  {
    ShowMSG(1,(int)"Freetype init error");
    return;
  }

  FT_New_Face(library,FONT_PATH,0,&face);
  if ( error == FT_Err_Unknown_File_Format )
  {
    ShowMSG(1,(int)"Unknown font format!");
    return;
  }
  else if ( error )
  {
    ShowMSG(1,(int)"Can`t open font!");
    return;
  }

  if(FT_Set_Pixel_Sizes( face, font_size, font_size )) ShowMSG(1, (int)"SetSize Error!");
  
  space_width = TT_GetCharWidth(0x20);
}

void DestroyFont()
{
  TT_FreeCache();
  FT_Done_Face(face);
  FT_Done_FreeType(library);
}
